package com.yatang.yiliao.app.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.yatang.yiliao.app.MyApp;
import com.yatang.yiliao.app.R;
import com.yatang.yiliao.app.utils.DataProvider;
import com.yatang.yiliao.app.utils.MyUtils;
import com.yatang.yiliao.app.utils.UserHttpClass;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

import java.util.List;

public class LoginActivity extends Activity implements View.OnClickListener {
    public static final int MSG_WHAT_CHECK_LOGIN_USER = 0x01;
    private EditText uNameEdit;
    private EditText uPassEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        uNameEdit = (EditText)findViewById(R.id.uNameEdit);
        uPassEdit = (EditText)findViewById(R.id.uPassEdit);
        Button login = (Button)findViewById(R.id.loginBtn);
        Button register = (Button)findViewById(R.id.registerBtn);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.loginBtn:
                login();
                break;
            case R.id.registerBtn:
                startActivity(new Intent(this, PhoneRegisterActivity.class));
                break;
        }

    }

    private void login() {
        UserHttpClass.newInstance().loginUser(this, refreshHandler,
                uNameEdit.getText().toString(), uPassEdit.getText().toString());
    }

    private Handler refreshHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_WHAT_CHECK_LOGIN_USER:
                    if ((Boolean) msg.obj) {
                        connectRongYunServer();
                        //获取当前用户会话列表
                        startActivity(new Intent(LoginActivity.this, SetInformActivity.class));
                    }
                    break;

                default:
                    break;
            }
        };
    };

    private void connectRongYunServer(){
        MyApp myApp = (MyApp)getApplicationContext();
        try {
            RongIM.connect(myApp.globelUser.getToken(), new RongIMClient.ConnectCallback() {

                @Override
                public void onError(ErrorCode arg0) {
                    Toast.makeText(getApplicationContext(),
                            "登录失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onSuccess(String arg0) {
                    Toast.makeText(getApplicationContext(),
                            "登录成功", Toast.LENGTH_SHORT).show();
                    initRongImData();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initRongImData(){
        final MyApp myApp = (MyApp)getApplicationContext();
        //如果说你的程序集成了会话列表的功能，那么这个方法就是初始化你的用户信息，同步到融云服务器
        RongIM.getInstance().setGetUserInfoProvider(new RongIM.GetUserInfoProvider() {
            @Override
            public RongIMClient.UserInfo getUserInfo(String arg0) {
                return new RongIMClient.UserInfo(myApp.globelUser.getUserId(), myApp.globelUser.getUsername(), "");
            }
        }, false);
        //初始化好友关系
        RongIM.setGetFriendsProvider(new RongIM.GetFriendsProvider() {
            @Override
            public List<RongIMClient.UserInfo> getFriends() {
                return MyUtils.fortmatUserToUserInfo(DataProvider.getTestUserData());
            }
        });
    }
}
